<template>
  <div>
    <div style="float: left;padding-top: 5px;">{{f_value_name}}</div>
    <van-image v-if="!disabled" height="24" style="margin-left: 20px;" @click="showEdit=true"
                src="/images/mob-icon-vf-04.svg"/>
    <van-popup v-model="showEdit" position="bottom" :style="{height:'90%'}" closeable >
      <div style="margin-top: 70px;" v-f="showEdit==true">
        <van-button style="float: right;margin-right: 20px;" size="small" type="danger" native-type='button'
                    @click="setValue">确定</van-button>
        <van-cascader v-loading="loading" v-model="s_value" title="请选择部门" :options="orgData" :closeable="false"/>
      </div>
    </van-popup>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading:false,
      showEdit:false,
      f_value_name:'',

      itemList:[],
      nameList:[],
      orgData:[],

      s_value:'',
    }
  },
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    value: { type: String, default: null },
    disabled:{ type: Boolean, default: false },
  },
  created() {},
  mounted() {
    this.s_value = this.value;
    this.load();
  },
  methods: {
    async load(){
      this.loading = true;
      if(!this.em(this.value)){
        let cList = await elm.post('/system/login/GetOrgPath', { id: this.s_value });
        if(cList.length>=0){
          this.f_value_name = cList[0].name;
        }
      }

      if(this.orgData.length==0){
        let orgData = await elm.post('/system/login/GetOrgTree',{});
        this.orgData = this.v_getOption(orgData);
        //console.log(JSON.stringify(this.orgData));
      }

      this.loading = false;
    },
    async setValue(){
      let tData = await elm.post('/system/login/GetOrgPath', { id: this.s_value });
      tData = tData[0];
      this.f_value_name = tData.name;
      //this.$emit('update:value', this.itemList[index].id);
      this.$emit('change', tData.id);
      this.showEdit = false;
    },

    v_getOption:function(list){
      let option = [];
      if(list!=null && list.length>0){
        for(let i= 0;i<list.length;i++){
          let itemI = list[i];
          let optionI = {text: itemI.name,value: itemI.id};
          if(itemI.children!=null && itemI.children.length>0){
            optionI['children'] = [];
            for(let j= 0;j<itemI.children.length;j++){
              let itemJ = itemI.children[j];
              let optionJ = {text: itemJ.name,value: itemJ.id};
              if(itemJ.children!=null && itemJ.children.length>0){
                optionJ['children'] = [];
                for(let k= 0;k<itemJ.children.length;k++){
                  let itemK = itemJ.children[k];
                  let optionK = {text: itemK.name,value: itemK.id};
                  if(itemK.children!=null && itemK.children.length>0){
                    optionK['children'] = [];
                    for(let ii= 0;ii<itemK.children.length;ii++){
                      let itemII = itemK.children[ii];
                      let optionII = {text: itemII.name,value: itemII.id};
                      optionK.children.push(optionII);
                    }
                  }
                  optionJ.children.push(optionK);
                }
              }
              optionI.children.push(optionJ);
            }
          }
          option.push(optionI);
        }
      }
      return option;
    },

    em: function (s) {
      if (s == null || s == '') { return true; }
      else { return false; }
    },
  }
}
</script>